package com.tengju.user.application.chief;

import com.google.common.collect.Lists;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.application.shared.ApplicationExceptionCode;
import com.tengju.user.domain.model.chief.*;
import com.tengju.user.domain.model.club.ClubId;
import com.tengju.user.domain.model.user.SsoUserId;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.model.user.StaffInfo;
import com.tengju.user.domain.service.DepartmentExternalService;
import com.tengju.user.domain.shared.JsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/3/13
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ChiefClubService {

    private final ChiefClubRepository chiefClubRepository;
    private final DepartmentExternalService departmentExternalService;

    public Boolean addClub(Staff staff, ChiefId chiefId, List<Long> clubIds) {
        if (CollectionUtils.isEmpty(clubIds)) {
            return true;
        }
        List<ChiefClub> existChiefClubList = chiefClubRepository.getChiefClub(chiefId);
        Map<ClubId, ChiefClub> existChiefClubMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(existChiefClubList)) {
            existChiefClubMap = existChiefClubList.stream()
                    .collect(Collectors.toMap(ChiefClub::getClubId, v -> v, (v1, v2) -> v1));
        }
        List<ChiefClub> newChiefClubs = new ArrayList<>();
        for (Long clubIdValue : clubIds) {
            ClubId clubId = new ClubId(clubIdValue);
            if (existChiefClubMap.containsKey(clubId)) {
                continue;
            }
            newChiefClubs.add(new ChiefClub(
                    chiefId,
                    clubId,
                    staff.getName(),
                    staff.getName()));
        }
        chiefClubRepository.batchInsert(newChiefClubs);
        return true;
    }

    public Boolean removeClub(ChiefId chiefId, List<Long> clubIds) {
        if (CollectionUtils.isEmpty(clubIds)) {
            return true;
        }
        chiefClubRepository.batchRemove(chiefId, clubIds);
        return true;
    }

    public void removeChiefClubRelation(ChiefClubRelationId chiefClubRelationId) {
        if (chiefClubRelationId == null) {
            return;
        }
        chiefClubRepository.removeChiefClubRelation(chiefClubRelationId);
    }

    public Boolean addChiefClubRelation(Staff staff, ChiefId chiefId, List<Long> clubIds) {
        if (!departmentExternalService.checkStaffInfo(new SsoUserId(chiefId.getValue()))) {
            throw ApplicationException.error(ApplicationExceptionCode.CHIEF_CODE_NOT_EXIST, chiefId.toString());
        }
        checkExistChiefClubRelation(chiefId, clubIds);
        return addClub(staff, chiefId, clubIds);
    }


    public void checkExistChiefClubRelation(ChiefId chiefId, List<Long> clubIds) {
        List<ChiefClubRelation> chiefClubRelations = chiefClubRepository.queryByClubIdAndChiefIdPage(new ClubId(clubIds.get(0)),
                chiefId, 0, 10);
        if (!CollectionUtils.isEmpty(chiefClubRelations)) {
            throw ApplicationException.error(ApplicationExceptionCode.CHIEF_CLUB_RELATION_EXIST, chiefId);
        }
    }
}
